<?php
declare (strict_types = 1);

namespace app\applet\middleware;

class AppKey
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $herd = $request->header();
        if(empty($herd['time']) || empty($herd['token']) ){
            exit(json_encode([
                'code'=> 500,
                'message'=> '缺少加密参数：'.getRandomWord(10),
                'data'=> null,
                'type'=> '',
            ]));
            die();
        }


        $role_key = config('app')['role_key'];
        $key = md5($herd['time'].''.$role_key);
        $tiken = md5($role_key.$key);
        if($tiken !== $herd['token']){
            exit( ret_json_encode(500,'请重新登录',null,'error'));
        }
        return $next($request);
    }
}

